package com.chandra.pos.web.sales.action;

import com.chandra.pos.EntityNotFoundException;
import com.chandra.pos.dao.entity.*;
import com.chandra.pos.model.OrderFilter;
import com.chandra.pos.security.UserDetailsImpl;
import com.chandra.pos.service.OrderManagement;
import com.chandra.pos.service.ProductService;
import com.chandra.pos.service.UserManagement;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.security.core.context.SecurityContextHolder;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * UserEntity: chkumar
 * Date: 5/22/12
 * Time: 11:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class SalesAction extends ActionSupport implements SessionAware {

    private UserManagement userManagementImpl;

    private ProductService productService;

    private OrderManagement orderManagement;

    private Map session;

    private OrderEntity orderEntity;
    private List<OrderEntity> orderEntityList;


    public OrderManagement getOrderManagement() {
        return orderManagement;
    }

    public void setOrderManagement(OrderManagement orderManagement) {
        this.orderManagement = orderManagement;
    }

    public UserManagement getUserManagementImpl() {
        return userManagementImpl;
    }

    public void setUserManagementImpl(UserManagement userManagementImpl) {
        this.userManagementImpl = userManagementImpl;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public String doNew() {

        this.orderEntity = (OrderEntity) session.get("orderEntity");

        if (this.orderEntity == null) {
            this.orderEntity = new OrderEntity();
            orderEntity.setId(-1);
            session.put("orderEntity", orderEntity);

        }
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetailsImpl) {

            orderEntity.setEmployee(((UserDetailsImpl) principal).getUser());
        }


        return INPUT;
    }

    public String doSave() {

        return SUCCESS;
    }

    public String doEdit() {

        return SUCCESS;
    }

    public String doUpdate() {

        orderManagement.updateOrder(this.orderEntity);
        return SUCCESS;
    }

    public String doList() {


        return SUCCESS;
    }

    public String doAddItem() {


        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        try {
            ProductEntity product = productService.getProduct(itemId);

            boolean found = false;

            for (OrderItemEntity itemEntity : orderEntity.getItemList()) {

                if (itemEntity.getProduct().getId() == product.getId()) {
                    itemEntity.setQuantity(itemEntity.getQuantity().add(new BigDecimal(1)));
                    found = true;
                    break;
                }

            }
            if (!found) {
                OrderItemEntity item = new OrderItemEntity();
                item.setQuantity(new BigDecimal(1));
                item.setSalePrice(product.getSalePrice());
                item.setProduct(product);
                item.setDiscount(new BigDecimal(0));


                orderEntity.getItemList().add(item);
            }


        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            addActionError("Unable to add item for sale!!");
        }


        //  gets


        return SUCCESS;
    }

    public String doEditItem() {
        ProductEntity product = null;
        try {
            product = productService.getProduct(itemId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();
        }

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        for (OrderItemEntity itemEntity : orderEntity.getItemList()) {

            if (itemEntity.getProduct().getId() == product.getId()) {
                itemEntity.setQuantity(quantity);

                itemEntity.setSalePrice(price);
                itemEntity.setDiscount(discount);

                break;
            }
        }

        return SUCCESS;
    }

    public String doDeleteItem() {
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        ProductEntity product = null;
        try {
            product = productService.getProduct(itemId);
            boolean found = false;

            for (OrderItemEntity itemEntity : orderEntity.getItemList()) {

                if (itemEntity.getProduct().getId() == product.getId()) {
                    orderEntity.getItemList().remove(itemEntity);

                    break;
                }

            }


        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return SUCCESS;
    }

    public String doAddShipping() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setShippingAmount(shippingAmount);
        return SUCCESS;
    }

    public String doDeleteShipping() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setShippingAmount(null);
        return SUCCESS;
    }

    public String doDelete() {
        try {
            OrderEntity orderEntity = orderManagement.getOrder(orderId);
            orderManagement.deleteOrder(orderEntity);

        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return SUCCESS;
    }

    public String doDeleteDiscount() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setDiscount(null);
        return SUCCESS;
    }

    public String doAddPayment() {


        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        boolean found = false;

        for (OrderPaymentEntity itemEntity : orderEntity.getPaymentList()) {

            if (itemEntity.getType().getId() == paymentType) {

                itemEntity.setAmount(itemEntity.getAmount().add(paymentAmount));
                found = true;
                break;
            }

        }
        if (!found) {
            OrderPaymentEntity payment = new OrderPaymentEntity();
            payment.setAmount(paymentAmount);
            payment.setType(new PaymentTypeEntity(paymentType));

            orderEntity.getPaymentList().add(payment);
        }

        return SUCCESS;
    }

    public String doDeletePayment() {
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");


        for (OrderPaymentEntity itemEntity : orderEntity.getPaymentList()) {

            if (itemEntity.getType().getId() == paymentType) {

                orderEntity.getPaymentList().remove(itemEntity);

                break;
            }

        }

        return SUCCESS;
    }


    public String doAddCustomer() {
        CustomerEntity userEntity = null;
        try {
            userEntity = userManagementImpl.getCustomer(customerId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setCustomer(userEntity);

        return SUCCESS;
    }

    public String doAddComment() {
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setNote(comment);

        return SUCCESS;
    }

    public String doAddDiscount() {
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setDiscount(discount);

        return SUCCESS;
    }


    public String doDeleteCustomer() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");

        orderEntity.setCustomer(null);

        return SUCCESS;
    }

    public String doListSuspended() {
        OrderFilter filter = new OrderFilter(0, 10);
        filter.setStatus(new OrderStatusEntity(2));
        this.orderEntityList = orderManagement.getOrders(filter);

        return SUCCESS;
    }

    public String doSuspend() {
        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setOrderStatus(new OrderStatusEntity(2, "Suspended"));
        orderManagement.createOrder(orderEntity);
        session.remove("orderEntity");
        return SUCCESS;
    }

    public String doCancel() {

        session.remove("orderEntity");
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(-1);
        session.put("orderEntity", orderEntity);
        return SUCCESS;
    }

    public String doComplete() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setNote(comment);
        orderEntity.setOrderStatus(new OrderStatusEntity(1, "Completed"));
        // session.remove("orderEntity");


        if (orderEntity.getId() != -1)
            this.orderEntity = orderManagement.updateOrder(orderEntity);
        else

            this.orderEntity = orderManagement.createOrder(orderEntity);

        session.remove("orderEntity");
        return SUCCESS;
    }

    public String doLoad() {

        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");
        orderEntity.setNote(comment);

        // session.remove("orderEntity");
        return SUCCESS;
    }

    public String doReceipt() {
        return SUCCESS;
    }

    public String doUnSuspend() {


        OrderEntity orderEntity = (OrderEntity) session.get("orderEntity");


        try {
            this.orderEntity = orderManagement.getOrder(getOrderId());
            session.put("orderEntity", this.orderEntity);
        } catch (EntityNotFoundException e) {

            return ERROR;
        }


        return SUCCESS;
    }
     public String changeMode() {




        return SUCCESS;
    }

    private String mode="sale";
    private String comment;

    private long orderId;

    private long customerId;

    private long itemId;

    private BigDecimal price;

    private BigDecimal discount;

    private BigDecimal quantity;
    private BigDecimal shippingAmount;

    private BigDecimal paymentAmount;

    private long paymentType;

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public BigDecimal getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(BigDecimal paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public long getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(long paymentType) {
        this.paymentType = paymentType;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public long getItemId() {
        return itemId;
    }

    public void setItemId(long itemId) {
        this.itemId = itemId;
    }

    @Override
    public void setSession(Map<String, Object> stringObjectMap) {
        this.session = stringObjectMap;
    }

    public OrderEntity getOrderEntity() {
        return orderEntity;
    }

    public void setOrderEntity(OrderEntity orderEntity) {
        this.orderEntity = orderEntity;
    }

    public BigDecimal getShippingAmount() {
        return shippingAmount;
    }

    public void setShippingAmount(BigDecimal shippingAmount) {
        this.shippingAmount = shippingAmount;
    }

    public List<OrderEntity> getOrderEntityList() {
        return orderEntityList;
    }

    public void setOrderEntityList(List<OrderEntity> orderEntityList) {
        this.orderEntityList = orderEntityList;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }
}
